package com.zk.iotgateway.component;

import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * ${DESCRIPTION}
 *
 * @author:mn.m
 * @create 2019-05-07 上午 11:32
 **/
@Component
public class FileUploadService {

    public  void upload(MultipartFile file, String clsPath){
        if (file==null||clsPath==null){
            return;
        }
        clsPath=clsPath.substring(0,clsPath.lastIndexOf("."));
        clsPath=clsPath.replace(".","/");
        clsPath=clsPath.concat("/").concat(file.getOriginalFilename());
        String root=this.getClass().getResource("/").getPath();
        String newPath=root.concat(clsPath);
        System.out.println(root);
        System.out.println(newPath);
        File target=new File(newPath);
        if (!target.getParentFile().exists()){
            target.getParentFile().mkdirs();
        }
        try {
            file.transferTo(target);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
